function ccR_Monk_Jab()
	if ccrPlayerHasSpell(JAB)
	and ccrSpellCanAttack(JAB) 
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(JAB)
		return
	end
end

function ccR_Monk_Tiger_Palm()
	-- Tiger Palm Proc
	if ccrPlayerHasSpell(TIGER_PALM)
	and ccrPlayerHasBuff(COMBO_BREAKER_TIGER_PALM)
	and ccrSpellCanAttack(COMBO_BREAKER_TIGER_PALM)
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(TIGER_PALM)
		return
	end

	-- Tiger Palm
	if ccrChi() < 1 then
		ccR_Monk_Jab()
	else
		if ccrSpellCanAttack(TIGER_PALM)
		and ccrPlayerIsFacingTarget()
		then
			ccrCastAttack(TIGER_PALM)
			return
		end
	end
end

function ccR_Monk_Fists_of_Fury()
	-- Use Fists of Fury
	if ccrPlayerHasSpell(FISTS_OF_FURY)
	and ccRS.Monk_3_UseFistsOfFury 
	and not onCD(FISTS_OF_FURY)
	and not ccrPlayerIsMoving()
	then
		if ccrChi() < 3 then
			ccR_Monk_Jab()
			return
		else
			if ccrSpellIsReady(FISTS_OF_FURY) then
				ccrCast(FISTS_OF_FURY)
				return
			end
		end
	end
end

function ccR_Monk_Blackout_Kick()
	-- Blackout Kick Proc
	if ccrPlayerHasSpell(BLACKOUT_KICK)
	and ccrPlayerHasBuff(COMBO_BREAKER_BLACKOUT_KICK)
	and ccrSpellCanAttack(BLACKOUT_KICK)
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(BLACKOUT_KICK)
		return
	end

	-- Blackout Kick
	if ccrChi() < 2 then
		ccR_Monk_Jab()
		return
	else	
		if ccrSpellCanAttack(BLACKOUT_KICK)
		and ccrPlayerIsFacingTarget()
		then
			ccrCastAttack(BLACKOUT_KICK)
			return
		end
	end
end

function ccR_Monk_Detox()
	if ccrPlayerHasSpell(DETOX)
	and ccRS.Monk_UseDetox
	and ccrSpellIsReady(DETOX)
	then
		-- Scan for a debuff for Poison/Disease
		local i = 1
		for i = 1, 40 do
			local name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId  = UnitDebuff("player", i) 
			-- No debuff found, Exit the check
			if name == nil then
				break
			else
				-- Found a debuff, Check if Poison or Disease
				if debuffType == "Poison"
				or debuffType == "Disease"
				then
					ccrCast(DETOX, "player")
					return
				end
			end
		end
		return
	end
end

function ccR_Monk_Touch_of_Death()
	if ccrPlayerHasSpell(TOUCH_OF_DEATH)
	and ccrPlayerHasBuff(DEATH_NOTE)
	and not ccrTargetIsEnemyPlayer()		
	and not ccrTargetIsBoss()
	and ccrPlayerIsFacingTarget()
	and ccrSpellIsReady(TOUCH_OF_DEATH)
	and UnitHealth("target") < UnitHealth("player")
	then
		-- Glyph of Touch of Death
		if ccrPlayerHasGlyph(123391) then
			if ccrSpellCanAttack(JAB) then
				ccrCastAttack(TOUCH_OF_DEATH)
				return
			end
		else
			-- Touch of Death
			if ccrChi() < 3 then
				ccR_Monk_Jab()
				return
			else	
				if ccrSpellCanAttack(TOUCH_OF_DEATH) then
					ccrCastAttack(TOUCH_OF_DEATH)
					return
				end
			end
		end
	end
	return
end

function ccR_Monk_Touch_of_Karma()
	if ccrPlayerHasSpell(TOUCH_OF_KARMA)
	and ccRS.Monk_3_UseTouchOfKarma
	and ccrPlayerHealth() <= ccRS.Monk_3_TouchOfKarmaHP
	and ccrSpellCanAttack(TOUCH_OF_KARMA)
	then
		ccrCastAttack(TOUCH_OF_KARMA)
	end
	return
end

function ccR_Monk_Legacy_of_the_Emperor()
	if ccrPlayerHasSpell(LEGACY_OF_THE_EMPEROR)
	and not ccrPlayerHasBuff(BLESSING_OF_KINGS)
	and not ccrPlayerHasBuff(BLESSING_OF_MIGHT)
	and not ccrPlayerHasBuff(1126) -- Mark of the Wild
	and not ccrPlayerHasBuff(LEGACY_OF_THE_EMPEROR) -- Legacy of the Emperor
	and not ccrPlayerHasBuff(90363) -- Embrace of the Shale Spider 
	then
		ccrCast(LEGACY_OF_THE_EMPEROR)
		return
	end
end

function ccR_Monk_Fortifying_Brew(specNumber)
	if specNumber == nil then specNumber = ccrPlayerSpec() end
	local vUseFortifyingBrew = ccRS["Monk_" .. specNumber .. "_UseFortifyingBrew"]	
	local vFortifyingBrewHP = ccRS["Monk_" .. specNumber .. "_FortifyingBrewHP"]

	if ccrPlayerHasSpell(FORTIFYING_BREW)
	and vUseFortifyingBrew
	and ccrPlayerHealth() <= vFortifyingBrewHP
	and ccrSpellIsReady(FORTIFYING_BREW)
	then
		ccrCast(FORTIFYING_BREW)
		return
	end
end

function ccR_Monk_Expel_Harm(specNumber)
	if specNumber == nil then specNumber = ccrPlayerSpec() end
	local vUseExpelHarm = ccRS["Monk_" .. specNumber .. "_UseExpelHarm"]	
	local vExpelHarmHP = ccRS["Monk_" .. specNumber .. "_ExpelHarmHP"]

	if ccrPlayerHasSpell(EXPEL_HARM)
	and vUseExpelHarm
	and ccrPlayerHealth() <= vExpelHarmHP
	and ccrSpellIsReady(EXPEL_HARM)
	then
		ccrCast(EXPEL_HARM)
		return
	end
end

function ccR_Monk_Disable()
	if ccrTargetHasMyDebuff(DISABLE_ROOTED) then return end

	if ccrPlayerHasSpell(DISABLE)
	and ccRS.Monk_UseDisable
	and ccrSpellCanAttack(DISABLE)
	and ccrTargetIsMoving()
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(DISABLE)
		return
	end
end

function ccR_Monk_Nimble_Brew()
	if ccrPlayerHasSpell(NIMBLE_BREW)
	and ccRS.Monk_UseNimbleBrew
	and ccrSpellIsReady(NIMBLE_BREW)
	and ccrPlayerIsInCombat()
	then
		local eventIndex = C_LossOfControl.GetNumEvents()
		while (eventIndex > 0) do
			local _, _, text = C_LossOfControl.GetEventInfo(eventIndex)
			if text == LOSS_OF_CONTROL_DISPLAY_ROOT 
			or text == LOSS_OF_CONTROL_DISPLAY_SNARE
			or text == LOSS_OF_CONTROL_DISPLAY_STUN_MECHANIC
			or text == LOSS_OF_CONTROL_DISPLAY_STUN
			or text == LOSS_OF_CONTROL_DISPLAY_FEAR
			or text == LOSS_OF_CONTROL_DISPLAY_HORROR
			then
				ccrCast(NIMBLE_BREW)
				return
			end
			eventIndex = eventIndex - 1
		end	
	end
end

function ccR_Monk_Spear_Hand_Strike()
	if ccrPlayerHasSpell(SPEAR_HAND_STRIKE)
	and ccRS.Monk_UseSpearHandStrike
	and ccrSpellIsReady(SPEAR_HAND_STRIKE)
	and ccrTargetIsCastingOrChanneling()
	and ccrTargetIsInteruptable()
	and ccrPlayerIsFacingTarget()
	then
		if ccrTargetIsChanneling() then ccrCastAttack(SPEAR_HAND_STRIKE); return; end
		if ccrTargetIsCasting()
		and ccrTargetHasLeftOnCast(1)
		then
			ccrCastAttack(SPEAR_HAND_STRIKE)
			return
		end
	end
end

function ccR_Monk_Energizing_Brew()
	if ccrPlayerHasSpell(ENERGIZING_BREW)
	and ccRS.Monk_3_UseEnergizingBrew
	and ccrEnergy() <= 40
	and ccrSpellIsReady(ENERGIZING_BREW)
	and ccrPlayerIsInCombat()
	then
		ccrCast(ENERGIZING_BREW)
		return
	end
end

function ccR_Monk_Spinning_Crane_Kick()
	if ccrPlayerHasSpell(SPINNING_CRANE_KICK)
	and ccrSpellIsReady(SPINNING_CRANE_KICK)
	then
		ccrCast(SPINNING_CRANE_KICK)
		return
	end
end

function ccR_Monk_Rising_Sun_Kick()
	if ccrChi() < 2 then
		ccR_Monk_Jab()
		return
	end
	if ccrPlayerHasSpell(RISING_SUN_KICK)
	and ccrSpellCanAttack(RISING_SUN_KICK)
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(RISING_SUN_KICK)
		return
	end
end

function ccR_Monk_Tiger_Eye_Brew()
	if specNumber == nil then specNumber = ccrPlayerSpec() end
	local vUseTigerEyeBrew = ccRS["Monk_" .. specNumber .. "_UseTigerEyeBrew"]	
	local vTigerEyeBrewStacks = ccRS["Monk_" .. specNumber .. "_TigerEyeBrewStacks"]

	if ccrPlayerHasSpell(TIGER_EYE_BREW)
	and ccrSpellIsReady(TIGER_EYE_BREW)
	and vUseTigerEyeBrew
	and ccrPlayerHasBuffStacks(TIGER_EYE_BREW_BUFF, vTigerEyeBrewStacks)
	then
		ccrCast(TIGER_EYE_BREW)
		return
	end
end

function ccR_Monk_Grapple_Weapon()
	if ccrPlayerHasSpell(GRAPPLE_WEAPON)
	and ccRS.Monk_UseGrappleWeapon
	and ccrSpellCanAttack(GRAPPLE_WEAPON)
	and ccrPlayerIsInCombat()
	and (vGrappleWeaponGUID == nil or (UnitGUID("target") ~= vGrappleWeaponGUID))
	and ccrPlayerIsFacingTarget()
	then
		ccrCastAttack(GRAPPLE_WEAPON)
		vGrappleWeaponGUID = UnitGUID("target")
		return
	end
end

function ccR_Monk_Healing_Sphere()
	if ccrPlayerHasSpell(HEALING_SPHERE)
	and ccRS.Monk_UseHealingSphere
	and ccrPlayerHealth() <= ccRS.Monk_HealingSphereHP
	and ccrSpellIsReady(HEALING_SPHERE)
	and ccrEnergy() >= 40
	then
		if SpellIsTargeting() == 1 then RunMacroText("/stopspelltarget") end
		ccrCast(HEALING_SPHERE)
		if SpellIsTargeting() == 1 then CastAtLocation(Player:GetLocation()) end
		return
	end
end

HasGhost = {}
tblTapped = {}
function ccR_Monk_Storm_Earth_and_Fire()
	if ccrPlayerHasSpell(STORM_EARTH_AND_FIRE)
	and ccRS.Monk_3_UseStormEarthAndFire
	then
		if ccrValidTarget()
		and (vTemp_LastSEFCheck == nil or (GetTime()-vTemp_LastSEFCheck > 0.5))
		then
			local objNPC = tblTGetNearbyEnemies
			if #objNPC > 1
			and not ccrPlayerHasBuff("Storm, Earth, and Fire")
			then
				if objNPC ~= nil then
					for i = 1, # objNPC do
						if ccrPlayerHasBuff(STORM_EARTH_AND_FIRE) then return end
						if objNPC[i]:IsTappedByPlayer()
						and objNPC[i]:GetGUID() ~= Target:GetGUID()
						and objNPC[i]:InLineOfSight()
						then
							if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
							objNPC[i]:CastSpellByID(STORM_EARTH_AND_FIRE)
							return
						end
					end
				end
			end
			vTemp_LastSEFCheck = GetTime()
		end
	end
end

function ccR_Monk_Legacy_of_the_White_Tiger()
	if ccrPlayerHasSpell(LEGACY_OF_THE_WHITE_TIGER)
	and not ccrPlayerHasBuff(LEGACY_OF_THE_WHITE_TIGER)
	and not ccrPlayerHasBuff(17007) -- Leader of the Pack
	and not ccrPlayerHasBuff(1459) -- Arcane Brilliance
	and not ccrPlayerHasBuff(61316) -- Dalaran Brilliance
	and ccrSpellIsReady(LEGACY_OF_THE_WHITE_TIGER)
	then
		ccrCast(LEGACY_OF_THE_WHITE_TIGER)
		return
	end
end